#include <tenacitas.communication/switchboard.h>
#include <tenacitas.log/text_file.h>
#include <cstdint>
#include <iostream>
#include <unistd.h>
#include <utility>
#include <chrono>
#include <thread>

namespace tenacitas {
    namespace communication {
        namespace test00 {

            struct channel {
                channel(){}
                

                int16_t read() {
                    return ++m_i;
                }

                void write(int16_t p_i) {
                    std::cout << p_i << std::endl;
                }
            private:
                static int16_t m_i;
            };

            int16_t channel::m_i = 9;
                
            struct receiver {
                bool start() {
                    return true;
                }
                    
                channel && wait_for_call() {
                    std::chrono::milliseconds dura( 2000 );
                    std::this_thread::sleep_for( dura );    
                    return std::move(channel());
                }

                void stop(){}
            };

            struct handler {
                bool operator()(channel && p_channel) {
                    int16_t l_i = p_channel.read();
                    p_channel.write(l_i + 12);
                    return true;
                }
                
            };
                
                        
            static bool f0() {
                typedef switchboard<receiver
                                    ,channel
                                    ,handler
                                    ,log::text_file> switchboard;

                log::text_file::config("test00",
                                       static_cast<int64_t>( getpid( ) ) );

                receiver l_receiver;
                handler l_handler;
                

                switchboard l_switchboard(std::move(l_receiver),
                                          std::move(l_handler)); 

                l_switchboard.start_sync();
                
                return true;
            }


            static bool run() {
                if(!f0()) {
                    return false;
                }

                return true;
            }

            
        }
    }
}
